package com.ctgu.server.controller;


import com.ctgu.server.dao.constant.CacheKey;
import com.ctgu.server.dao.dto.FormUserDto;
import com.ctgu.server.dao.dto.FromDto;
import com.ctgu.server.dao.dto.GroupDto;
import com.ctgu.server.dao.entity.Form;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.service.FormService;
import com.ctgu.server.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ctgu
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/server/form")
@CrossOrigin
@Api(tags = "表单操作接口")
@Slf4j
public class FormController {


    @Autowired
    private FormService formService;

    @Autowired
    private CacheChannel cacheChannel;


    @GetMapping("/list")
    @ApiOperation(notes = "获取所有表单",value = "获取所有表单")
    public Result getFormList(){
        //查询缓存是否存在
        List<Form> formList= (List<Form>) cacheChannel.get(CacheKey.Form_Redis,"list").getValue();
        if(formList==null){
            log.info("进入数据库");
            formList= formService.list();
            cacheChannel.set(CacheKey.Form_Redis,"list",formList);
        }

        return ResultUtil.success(formList);
    }
    @GetMapping("/list/{id}")
    @ApiOperation(notes = "获取用户相关的表单",value = "获取用户相关的表单")
    public Result getUserFormList(@NotEmpty(message = "id不能为空") @PathVariable String id){
        return formService.getUserFormList(id);
    }
    @GetMapping("/userList")
    @ApiOperation(notes = "获取创建的表单",value = "获取用户创建的表单")
    public Result getUserCraeteList(@RequestParam String userId){
        if(userId==null){
            return ResultUtil.error("参数错误！");
        }
        return formService.getUserCrateList(userId);
    }
//    @GetMapping("/ApplyList")
//    @ApiOperation(notes = "获取用户已经报名的表单",value = "获取用户已经报名的表单")
//    public Result getUserApplyList(@RequestParam String userId){
//        if(userId==null){
//            return ResultUtil.error("参数错误！");
//        }
//        return formService.getUserApplyList(userId);
//    }
    @PostMapping("/create")
    @ApiOperation(notes = "GroupList为空则表示无任何限制",value = "创建表单")
    public Result createForm( @RequestBody FromDto fromDto){
        if(fromDto.getUserId()==null){
            return ResultUtil.error("参数为空");
        }
        return formService.createForm(fromDto);
    }

    @DeleteMapping("/delete")
    @ApiOperation(notes = "删除表单",value = "删除表单")
    public Result deleteForm(@RequestParam String userId,@RequestParam Long formId){
        if(userId==null||formId==null){
            return ResultUtil.success("参数不能为空");
        }
        return formService.deleteForm(userId,formId);
    }
    @PostMapping("/apply")
    @ApiOperation(notes = "用户报名",value = "用户报名")
    public Result userApplyForm( @RequestBody FormUserDto formUserDto){
       return formService.userApplyForm(formUserDto);
    }




}

